package com.tuanzi.loan.web.bean.family;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import lombok.Data;
import lombok.EqualsAndHashCode;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.FileUploadEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.base.Charsets;
import com.tuanzi.loan.business.entity.family.BlacklistType;
import com.tuanzi.loan.business.entity.family.UserBlacklist;
import com.tuanzi.loan.business.service.family.BlacklistTypeService;
import com.tuanzi.loan.business.service.family.UserBlacklistService;
import com.tuanzi.loan.web.configuration.JSFInfoBean;

/**
 * 黑名单导入处理
 * @author Xumiaohang 2017/11/16
 *
 */
@Data
@Slf4j
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class UserBlacklistBean extends JSFInfoBean<UserBlacklist> {

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private UserBlacklistService userBlacklistService;
	@Autowired
	private BlacklistTypeService blacklistTypeService;
	
	private List<BlacklistType> blacklistTypes;
	//黑名单类型
	private String type;
	// 有效的总数量
	private int totalCount;
	// 重复的手机数量
	private int repeatCount;
	// 临时存储文件中全部数据
	private List<String> totalList;
	// 临时存储手机号的集合
	private List<String> mobnumList;
	// 临时存储重复手机号的集合
	private List<String> repeatList;
	
	@PostConstruct
	private void init() {
		blacklistTypes=blacklistTypeService.findAll();
	
	}
	
	// 获取上传的文件
	public void handleBlacklist(FileUploadEvent event) {

		try {
			totalList = IOUtils.readLines(event.getFile().getInputstream(),
					Charsets.UTF_8);
			log.info("totalList.size = " + totalList.size());
			//文件中手机号去重
			mobnumList = totalList.stream().distinct().collect(Collectors.toList());
			log.info("mobnumList.size = " + mobnumList.size());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//校验文件
	public void onCheck(){
		//校验号码的有效性
		if (mobnumList!=null && mobnumList.size()>0) {
			Iterator<String> iterator = mobnumList.iterator();
			while(iterator.hasNext()){
				if(iterator.next().trim().length()!=11){
					iterator.remove();
				}
			}
			totalCount=mobnumList.size();
			repeatList = userBlacklistService.repeatCheck(mobnumList,type);
			repeatCount=repeatList.size();
			//去除重复的手机号
			mobnumList.removeAll(repeatList);
			addInfoMessage("校验完成！");
		}else if(mobnumList==null){
			 addWarnMessage("请先选择文件！");
		}
		
	}
	
	// 保存有效数据
	public void onSave() {
		 if (mobnumList!=null && mobnumList.size()>0) {
			 List<UserBlacklist> paramList=new ArrayList<UserBlacklist>();
			 for(String mobnum:mobnumList){
				 $ = new UserBlacklist();
				 $.setMobnum(mobnum);
				 $.setType(type);
				 paramList.add($);
			 }
			 userBlacklistService.saveOrUpdate(paramList);
			 addSuccessfullyMessage();
			 //重新初始化
			 totalCount=0;
			 repeatCount=0;
			 mobnumList=null;
		 }else{
			 addWarnMessage("没有数据导入！");
		 }
	}
}
